<?php
/*
 * 我的二维码
 */
namespace Mobile\Controller;
use Mobile\Model\gradeModel;

class QrcodeController extends BaseController {
    
    public $gradeModel;
    public $grade_arr;
    
    public function __construct() {
        parent::__construct();
        if(!$this->wechat){
            $this->wechat = new WechatController();
        }
        
        $this->gradeModel = new gradeModel();
        $this->grade_arr = $this->_get_gradename();
    }
    
    
    protected function _get_gradename(){
        $where = array();
        $grade_list = $this->gradeModel->getGradeList($where);
    
        $new_grade = array();
        foreach($grade_list as $k=>$v){
            $new_grade[$v['grade_id']] = $v['grade_name'];
        }
        return $new_grade;
    }
    
    
    /*
     * 我的二维码
     */
    public function index(){
        
        $seo_data = array(
            "title" => $this->lang['user_qrcode'],
            "description" => "",
            "keywords" => "",
        );
        $this->setSeo($seo_data);
    
        $share_data = array(
            "title" => $this->memberinfo['nickname']."的微商城",
        );
        $this->setShare($share_data);
    
    
        $userinfo = $this->memberinfo;
        //获取用户信息
        $wxuser = $this->wechat->getUserInfo($userinfo['openid']);
        
        if(!empty($wxuser)){
            $userinfo['nickname'] = $wxuser['nickname'] ? $wxuser['nickname'] : $userinfo['nickname'];
            $userinfo['headimgurl'] = $wxuser['headimgurl'] ? $wxuser['headimgurl'] : $userinfo['headimgurl'];
        }
    
        $share_pic = $this->makeshare($userinfo,1);//生成分享二维码
        if($share_pic){
            $this->assign("share_pic",$share_pic);
        }
    
        $this->display();
    }
    
    
    

    //获取二维码地址
    public function qrcodeurl($uid,$refresh=0){
        if(!$uid){
            $uid = $this->uid;
        }
        $text = get_domain()."/mobile/login/register?refer=".$uid;//内容
        $pic_url = "http://qr.liantu.com/api.php?text=".$text;//图片网址
        return $pic_url;

        $outfile = ROOT."/upload/qrcode/".$uid.".png";
        if(!file_exists($outfile) || $refresh){
    
            //qrcode($text, $outfile, $level = 1, $size = 8, $margin = 1,$saveandprint=false);
    
            $scene_id = "FENXIAO_UID_".$uid;
            $ticket_arr = $this->wechat->getQRCode($scene_id,2);//获取二维码ticket
            if(isset($ticket_arr['url'])){
                //getQRUrl($ticket)
                $text = $ticket_arr['url'];
                qrcode($text, $outfile, $level = 1, $size = 8, $margin = 1,$saveandprint=false);
            }
    
            if(!file_exists($outfile)){
                //不存在,生成失败
                return false;
            }
        }
        return $pic_url;
    }
    
    
    /*
     * 生成推广二维码图片
     */
    public function makeshare($userinfo,$refresh=0){
    
        $uid = $userinfo['uid'];
        $headimgurl = $userinfo['headimgurl'];
    
        //获取 等级
        $grade_arr = M('grade')->select();
        $grade_name = $this->grade_arr[$userinfo['grade_id']];
        
        if(!$headimgurl){
            $headimgurl = C("UPYUN_DOMAIN").C("SHARE_DEFAULT_IMG");//如果没有获取到头像就用logo代替
        }
    
        $nickname = $userinfo['realname'] ? $userinfo['realname'] : $userinfo['nickname'];
        if(!$nickname){
            $nickname = C("SITE_NAME");
        }
    
        $clean_text = emoji_docomo_to_unified($nickname);
        $html = emoji_unified_to_html($clean_text);
        $nickname = strip_tags($html);
    
        $nickname = "".$nickname;
    
        if(!$uid){
            $uid = $this->uid;
        }
        $file = ROOT.'/upload/share/'.$uid.".jpg";//存储的文件名
        $pic_url = get_domain()."/upload/share/".$uid.".jpg";
        if(file_exists($file) && !$refresh){
            //如果文件存在 并且不需要更新
            return $pic_url;
        }
    
        //获取二维码图片
        $qrcode_pic = $this->qrcodeurl($uid,$refresh);
        if(!$qrcode_pic){
            return false;//生成二维码失败
        }
    
        $read_qrcode = file_get_contents($qrcode_pic);//读取二维码
        $erweima = imagecreatefromstring($read_qrcode);//把二维码载入画板
        $qwidth = imagesx($erweima);//二维码图片宽度
        $qheight = imagesy($erweima);//二维码的图片高度
    
    
        $grade = $userinfo['grade_id'] ? $userinfo['grade_id'] : 1;
    
        $background_url = C("UPYUN_DOMAIN").C("QRCODE_GRADE".$grade);
    
        $read_backgroud = file_get_contents($background_url);//读取底图
    
        $background = imagecreatefromstring($read_backgroud);//把底图载入画板
    
        /*
         $dst_image：新建的图片
         $src_image：需要载入的图片
         $dst_x：设定需要载入的图片在新图中的x坐
         $dst_y：设定需要载入的图片在新图中的y坐标
         $src_x：设定载入图片要载入的区域x坐标
         $src_y：设定载入图片要载入的区域y坐标
         $dst_w：设定载入的原图的宽度（在此设置缩放）
         $dst_h：设定载入的原图的高度（在此设置缩放）
         $src_w：原图要载入的宽度
         $src_h：原图要载入的高度
        */
        imagecopyresampled($background,$erweima,580,1140,0,0,315,315,$qwidth,$qheight);//把二维码放到背景图片上
    
        $read_headimgurl = curl($headimgurl);//读取头像
        $headimg = imagecreatefromstring($read_headimgurl);//头像写入画板
    
        $head_width = imagesx($headimg);
        $head_height = imagesy($headimg);//获取头像宽高
    
        //imagecopyresampled($background,$headimg,50,77,0,0,190,190,$head_width,$head_height);//把头像写在花瓣是
    
        $font = ROOT.'/Static/mobile/font/siyuan.ttf';//思源黑体
    
        $color= imagecolorallocate($background,255,255,255);//颜色
    
        if(mb_strlen($nickname)>22){
            $font_size = 16;
        }else if(mb_strlen($nickname)>18){
            $font_size = 19;
        }else if(mb_strlen($nickname)>16){
            $font_size = 20;
        }else{
            $font_size = 22;
        }
        //imagettftext($background,$font_size,0,370,138,$color, $font,$nickname);//把名字写上去
     //   imagettftext($background,22,0,350,178,$color, $font,$grade_name);//把等级写上去
    
        imagejpeg($background,$file,100);//最后一个参数是图片质量,保存图片到文件
    
        if(file_exists($file)){
            //如果文件存在 并且不需要更新
            return $pic_url;
        }
    
        return false;
    }
    
    /*
     * 对外输出二维码
     * PUBLIC
     */
    public function userqrcode(){
        $uid = get("qrcode_uid");
        $userinfo = $this->getMemberInfobyUid($uid);
    
        if(empty($userinfo)){
            $this->error2("用户信息不存在");
        }
    
        //从公众平台拉取最新的资料
        if($this->is_wechat){
    
        }else{
            $this->wechat = new WechatController();
        }
        $wxuser = $this->wechat->getUserInfo($userinfo['openid']);
        if(!empty($wxuser)){
            $userinfo['nickname'] = $wxuser['nickname'] ? $wxuser['nickname'] : $userinfo['nickname'];
            $userinfo['headimgurl'] = $wxuser['headimgurl'] ? $wxuser['headimgurl'] : $userinfo['headimgurl'];
        }
    
        $share_pic = $this->makeshare($userinfo,$refresh=1);//生成分享二维码
    
        if($share_pic){
            //把图片输出到页面
            $background = file_get_contents($share_pic);
            header("Content-Type: image/jpeg");
            echo $background;
        }else{
            echo "生成二维码图片发生错误";
        }
    
    
    }
    
    

    /*
     * 我的纯二维码
     * /mobile/user/erweima?qrcode_uid=
     */
    public function erweima(){
        $uid = get("qrcode_uid");
        $userinfo = $this->getMemberInfobyUid($uid);
    
        if(empty($userinfo)){
            exit;
        }
    
        $share_pic = $this->qrcodeurl($uid);
    
        if($share_pic){
            //把图片输出到页面
            $background = file_get_contents($share_pic);
            header("Content-Type: image/jpeg");
            echo $background;
        }else{
            exit;
        }
    }
    
  
    /*
     * 提示关注
     */
    public function subscribe(){
    
        $seo_data = array(
            "title" => $this->lang['user_subscribe'],
            "description" => "",
            "keywords" => "",
        );
        $this->setSeo($seo_data);
    
        $share_data = array(
            "title" => $this->lang['user_subscribe'],
        );
        $this->setShare($share_data);
        
        
        if($this->memberinfo['grade_id']>1){
            $qrcode_uid = $this->memberinfo['uid'] ? $this->memberinfo['uid'] : 0;//关注二维码
        }else{
            $qrcode_uid = 0;//关注二维码
        }
        
        $this->assign('qrcode_uid',$qrcode_uid);
        
        $this->display();
    }
    
}